﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web;

namespace OrbitOne.Elmah.Presenter
{
    //TODO Filters op een andere manier (gewoon een linkje ??? ) !!!!

    public class MasterPage : System.Web.UI.MasterPage
    {
        #region Controls
        /// <summary>
        /// Head1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected HtmlHead Head1;

        /// <summary>
        /// form1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected HtmlForm form1;

        protected TextBox tbxSearch;
        protected ImageButton ibSearch;
        protected Repeater rptSearchFilters;
        protected Repeater MenuItems;
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeSearchBox();
            InitializeMenu();

        }

        private void InitializeMenu()
        {
            MenuItems.DataSource = new SiteMapDataSource{ShowStartingNode=false};
            MenuItems.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            rptSearchFilters.DataSource = Utility.GetAllSearchFilters(Context);
            rptSearchFilters.DataBind();
            base.OnPreRender(e);
        }

        protected void ibSearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(string.Format("Search.aspx?kw={0}&allapp=true&gv=3", tbxSearch.Text));
        }

        protected void rptSearchFilters_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var filter = Utility.GetSearchFilter(Context,
                                                 string.Format("SearchFilter{0}",
                                                 e.CommandArgument));
            Utility.SaveSearchFilter(Context, "DefaultSearchFilter", filter);
            Response.Redirect("Search.aspx?fsid=" + filter.Id);
        }

        private void InitializeSearchBox()
        {
            tbxSearch.Attributes.Add("value", "Search...");
            tbxSearch.Attributes.Add("onfocus", "if (this.value=='Search...') this.value = ''");
            tbxSearch.Attributes.Add("onkeydown", string.Format("if ((event.which && event.which == 13) ||(event.keyCode && event.keyCode == 13)){{document.getElementById('{0}').click();return false;}} else return true;", ibSearch.ClientID));
        }

        protected void BindMenuItem(object sender, RepeaterItemEventArgs args)
        {
            var node = args.Item.DataItem as SiteMapNode;
            if(node != null && (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem))
            {
                var link = new HyperLink
                               {
                                       NavigateUrl = node.Url,
                                       ToolTip = node.Description,
                                       Text = node.Title
                               };
                var li = new HtmlGenericControl("li");
                if (SiteMap.CurrentNode == node)
                {
                    li.Attributes.Add("class", "active");
                }
                li.Controls.Add(link);
                args.Item.Controls.Add(li);
            }
        }
    }
}
